package com.water.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.water.entity.PyBill;
import com.water.service.PyBillService;
import org.junit.runners.model.InvalidTestClassError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.PUT;
import java.util.*;

/**
 * (PyBill)表控制层
 *
 * @author makejava
 * @since 2020-12-22 18:39:21
 */
@RestController
@RequestMapping("pyBill")
public class PyBillController {
    /**
     * 服务对象
     */
    @Resource
    private PyBillService pyBillService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public PyBill selectOne(String id) {
        return this.pyBillService.queryById(id);
    }

    /*
     * 查询指定行数据
     * @param billno 主键
     * @return 影响行数
     */
    @RequestMapping("selectPyBill")
    public ModelAndView selectPyBill(ModelAndView model){
        List<Map<String,Object>> mapList = pyBillService.selectPyBill();
        int a = mapList.size();
        int b = 0;
        double db = 0;
        if(mapList.size() > 0){
            for(int i = 0;i < mapList.size();i++){
                db += (Double)mapList.get(i).get("BillMoney");
            }
            if(a%16 == 0){
                b = a/16;
            }else{
                b = a/16+1;
            }
            mapList.get(0).put("length",a);
            mapList.get(0).put("money",db);
            mapList.get(0).put("page",b);
            List<Map<String,Object>> mapList1 = new ArrayList<>();
            if (mapList.size() >16){
                for (int i = 0;i < 16;i++){
                    if(i == mapList.size()){
                        break;
                    }
                    Map<String,Object> map = new HashMap<>();
                    Iterator<Map.Entry<String,Object>> it = mapList.get(i).entrySet().iterator();
                    while (it.hasNext()){
                        Map.Entry<String,Object> m = it.next();
                        map.put(m.getKey(),m.getValue());
                    }
                    mapList1.add(map);
                }
                model.addObject("mapList",mapList1);
            }else{
                model.addObject("mapList",mapList);
            }
        }else{
            b = b + 1;
            Map<String,Object> map = new HashMap<>();
            map.put("length",a);
            map.put("money",db);
            map.put("page",b);
            map.put("BillMoney","");
            map.put("UserNo","");
            map.put("UserName","");
            map.put("Address","");
            map.put("Phone","");
            mapList.add(map);
            model.addObject("mapList",mapList);
        }
        model.setViewName("pay/pay_reportBalance");
        return model;
    }

    /*
     * 查询指定行数据
     * @param billno 主键
     * @return 影响行数
     */
    @GetMapping("MoneyselectPyBill")
    public ModelAndView MoneyselectPyBill(HttpServletRequest request, HttpServletResponse response, ModelAndView model){
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        Double money = Double.parseDouble(request.getParameter("money"));
        int num = Integer.parseInt(request.getParameter("num"));
        List<Map<String,Object>> mapList = pyBillService.MoneyselectPyBill(money);
        int a = mapList.size();
        int b = 0;
        double db = 0;
        if (mapList.size() >0){
            for(int i = 0;i < mapList.size();i++){
                db += (Double)mapList.get(i).get("BillMoney");
            }
            if(a%16 == 0){
                b = a / 16;
            }else{
                b = a / 16 + 1;
            }
            mapList.get(0).put("length",a);
            mapList.get(0).put("money",db);
            mapList.get(0).put("page",b);
            List<Map<String,Object>> mapList1 = new ArrayList<>();
            PageHelper.startPage(num,16);
            PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(mapList);
            model.addObject("mapList",mapList);
        }else{
            b = b + 1;
            Map<String,Object> map = new HashMap<>();
            map.put("length",a);
            map.put("money",db);
            map.put("page",b);
            map.put("BillMoney","");
            map.put("UserNo","");
            map.put("UserName","");
            map.put("Address","");
            map.put("Phone","");
            mapList.add(map);
            model.addObject("mapList",mapList);
        }
        model.setViewName("pay/pay_reportBalance");
        return model;
    }
}